package com.thinkgem.jeesite.modules.wechat.controller;

import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.github.sd4324530.fastweixin.api.response.GetUserInfoResponse;
import com.thinkgem.jeesite.common.config.Config;
import com.thinkgem.jeesite.common.config.GlobalConst;
import com.thinkgem.jeesite.common.utils.CacheUtils;
import com.thinkgem.jeesite.common.utils.IdGen;
import com.thinkgem.jeesite.common.utils.StringUtils;

import me.chanjar.weixin.common.exception.WxErrorException;

/**
 * 微信菜单操作
 * @author waile23
 *
 */
@RestController
@RequestMapping("${wechatPath}/rest/oauth")
public class WechatOauth2Controller {

   /**
    * 微信oauth认证连接（当使用前后端分离结构时可以使用本函数）
    * 微信重新向页面，微信菜单指向本地址
    * @param request
    * @param response
    * @return
    * @throws WxErrorException
    * @throws IOException
    */
    @RequestMapping(value = "/authorization", method = RequestMethod.GET)
    public void authorization(HttpServletRequest request, HttpServletResponse response) throws WxErrorException, IOException {
    	//默认跳转的前台页面
    	String frontRedirectPage = request.getParameter("m");
    	frontRedirectPage = StringUtils.isBlank(frontRedirectPage) ? "" : frontRedirectPage;

    	String sessionId = request.getSession().getId();

    	if(StringUtils.isBlank(sessionId)) {
    		return;
    	}

    	GetUserInfoResponse wxMpUser = (GetUserInfoResponse)CacheUtils.get(GlobalConst.WECHAT_USER_CACHE, sessionId);
    	if(wxMpUser!= null) {
    		//返回到前台的code值
    		String code = IdGen.uuid();

    		CacheUtils.put(GlobalConst.WECHAT_USER_CACHE, wxMpUser.getOpenid(), wxMpUser);
    		CacheUtils.put(GlobalConst.WECHAT_USER_CACHE, code, wxMpUser.getOpenid());

    		response.sendRedirect(Config.wechatRedirectUrl() + "/#/home?code=" + code + "&m=" + frontRedirectPage);
    	}
    }



    /**
    * 微信oauth认证连接（当使用前后端分离结构时可以使用本函数）
    * 微信重新向页面，微信菜单指向本地址
    * @param request
    * @param response
    * @return
    * @throws WxErrorException
    * @throws IOException
    */
    @RequestMapping(value = "/auth", method = RequestMethod.GET)
    public void auth(HttpServletRequest request, HttpServletResponse response) throws WxErrorException, IOException {
    	//默认跳转的前台页面
    	String frontRedirectPage = request.getParameter("m");
    	frontRedirectPage = StringUtils.isBlank(frontRedirectPage) ? "" : frontRedirectPage;

    	String sessionId = request.getSession().getId();

    	if(StringUtils.isBlank(sessionId)) {
    		return;
    	}

    	GetUserInfoResponse wxMpUser = (GetUserInfoResponse)CacheUtils.get(GlobalConst.WECHAT_USER_CACHE, sessionId);
    	if(wxMpUser!= null) {
    		//返回到前台的code值
    		String code = IdGen.uuid();

    		CacheUtils.put(GlobalConst.WECHAT_USER_CACHE, wxMpUser.getOpenid(), wxMpUser);
    		CacheUtils.put(GlobalConst.WECHAT_USER_CACHE, code, wxMpUser.getOpenid());

			String redirectPage = request.getParameter("redirecturl");
			String url = "http://192.168.183.115:8082";
//			String wd = "智能+学习";
			String encoderUrl = URLEncoder.encode(url, "UTF-8");
			System.out.println(encoderUrl);
			redirectPage = URLDecoder.decode(redirectPage, "UTF-8");
			System.out.println(redirectPage);


    		String RedirectUrl = redirectPage;//"http://192.168.183.115:8082";
    		response.sendRedirect(RedirectUrl + "/#/home?code=" + code + "&m=" + frontRedirectPage);
    	}
    }
}
